<?php
/**
 * 代录人操作控制类
 * User: Administrator
 * Date: 2018/10/31 0031
 * Time: 21:37
 */

namespace app\wx\controller;

use  think\Controller;
use  module\InsurerApi;
use think\Db;
use  think\Validate;
use  think\Request;
use  model\UserInfoModel;
use  library\Validator;
use  module\UserApi;
use model\PolicyModel;
class Recordor extends CommonController
{

    /**
     * 修改1
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function update1()
    {
//        $param = $this->request->param();
//        $info = Db::table('policy')->where('policy_num',$param['policy_num'])->where('recordor_id',$this->uid)->find();
//        $info['policy_name'] = $this->getInsurantType($info['insurance_type']);
//        $user = Db::table('user_info')->where('ui_id',$info['ui_id'])->find();
//        return $this->fetch('Recordor/update1',['info'=>$info,'user'=>$user]);
        $policy_num= input('get.policy_num');
        $policy_info=Db::table('policy')->where('policy_num',$policy_num)->where('recordor_id',$this->uid)->find();
        $p_id=$policy_info['p_id'];
        $info=[];
        $info['goods_type_name']='';
        if(!empty($policy_num)){
            $info = InsurerApi::getInfoByWherePolicyItem(['policy_num'=>$policy_num], ['goods_type,goods_name,goods_num,good_price,good_pg_price']);
            $typeInfo=Db::table('goods_type')->where('gt_id',$info['goods_type'])->find();
            $info['goods_type_name']=$typeInfo['gt_name'];
        }
        return $this->fetch('recordor/update1',['policy_num'=>$policy_num,'info'=>$info,'p_id'=>$p_id]);
    }
    public function update2()
    {
        $policy_num= input('get.policy_num');
        $policy_info=Db::table('policy')->where('policy_num',$policy_num)->where('recordor_id',$this->uid)->find();
        $p_id=$policy_info['p_id'];
        $info=[];
        if(!empty($policy_num)){
            $info = InsurerApi::getInfoByWherePolicyItem(['policy_num'=>$policy_num], ['damage_at,damage_result,omit,city,classify,address_detail,other_report']);
            $info['other_report']=$info['other_report']?json_decode( $info['other_report']):'';
            if( $info['other_report']){
                foreach ($info['other_report'] as $k =>$v){
                    if(!$v) continue;
                    preg_match('/\/([^\/]+\.[a-z]+)[^\/]*$/',$v,$match);
                    array_push($info['other_report'],['url'=>$v,'name'=>$match[1]]);
                    unset($info['other_report'][$k]);
                }
            }
        }

        $str='';
        if(isset($info['omit']) && $info['omit']){
            $str .=$info['omit'].' ';
        }
        if(isset($info['city']) && $info['city']){
            $str .=$info['city'].' ';
        }
        if(isset($info['classify']) && $info['classify']){
            $str .=$info['classify'];
        }

        return $this->fetch('recordor/update2',['policy_num'=>$policy_num,'info'=>$info,'addr'=>$str,'p_id'=>$p_id]);
    }
    /**
     *  修改页面 展示页面三
     */
    public  function update3(){
        $policy_num= input('get.policy_num');
        $policy_info=Db::table('policy')->where('policy_num',$policy_num)->where('recordor_id',$this->uid)->find();
        $p_id=$policy_info['p_id'];
        $info=[];
        if(!empty($policy_num)){
            $info = InsurerApi::getInfoByWherePolicyItem(['policy_num'=>$policy_num], ['present_at,goods_list,goods_desc,present_img,present_video']);
            $info['goods_list'] =  $info['goods_list']?json_decode($info['goods_list']):'';
            $info['present_video'] =  $info['present_video']?json_decode($info['present_video']):'';
            $info['present_img'] =  $info['present_img']?json_decode($info['present_img']):'';
            if( $info['goods_list']){
                foreach ($info['goods_list'] as $k =>$v){
                    if(!$v) continue;
                    preg_match('/\/([^\/]+\.[a-z]+)[^\/]*$/',$v,$match);
                    array_push($info['goods_list'],['url'=>$v,'name'=>$match[1]]);
                    unset($info['goods_list'][$k]);
                }
            }
        }
        return $this->fetch('recordor/update3',['policy_num'=>$policy_num,'info'=>$info,'p_id'=>$p_id]);
    }
    /**
     *  保险修改展示第四步
     */
    public  function update4(){
        $policy_num= input('get.policy_num');
        $policy_info=Db::table('policy')->where('policy_num',$policy_num)->where('recordor_id',$this->uid)->find();
        $p_id=$policy_info['p_id'];
        $info=[];
        if(!empty($policy_num)){
            $info = InsurerApi::getInfoByWherePolicyItem(['policy_num'=>$policy_num], ['should_at,rem_company,linkman,connect_phone']);
        }
        return $this->fetch('recordor/update4',['policy_num'=>$policy_num,'info'=>$info,'p_id'=>$p_id]);
    }
    /**
     * 录入
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function step_1()
    {
        $param = $this->request->param();
        $insurance_type_name='';
        $info = Db::table('policy')->where('policy_num',$param['policy_num'])->where('recordor_id',$this->uid)->find();
        if($info['insurance_type']){
            $insuranceTypeList=Db::table('insurance_type')->field('it_name')->where('it_id','in',$info['insurance_type'])->select();
            foreach ($insuranceTypeList as $v){
                $insurance_type_name.= $v['it_name'].',';
            }
            $insurance_type_name=rtrim($insurance_type_name,',');
        }
        $user = Db::table('user_info')->where('ui_id',$info['ui_id'])->find();
        return $this->fetch('recordor/step_1',['info'=>$info,'user'=>$user,'insurance_type_name'=>$insurance_type_name]);
    }

    public function step_2()
    {
        $param = $this->request->param();
        $res = Db::table('policy')->where('policy_num',$param['policy_num'])->where('recordor_id',$this->uid)->find();
        $info = Db::table('policy_item')->where('policy_num',$res['policy_num'])->find();
        $typeInfo=Db::table('goods_type')->where('gt_id',$info['goods_type'])->find();
        $info['goods_type_name']=$typeInfo['gt_name'];
        return $this->fetch('recordor/step_2',['info'=>$info,'policy_num'=>$res['policy_num']]);
    }
    /**
     *  保险录入 展示第三步
     */
    public  function step3(){
        $policy_num= input('get.policy_num');
        $info=[];
        if(!empty($policy_num)){
            $info = InsurerApi::getInfoByWherePolicyItem(['policy_num'=>$policy_num], ['damage_at,damage_result,omit,city,classify,address_detail,other_report']);
            $info['other_report']=$info['other_report']?json_decode( $info['other_report']):'';

            if( $info['other_report']){
                foreach ($info['other_report'] as $k =>$v){
                    if(!$v) continue;
                    preg_match('/\/([^\/]+\.[a-z]+)[^\/]*$/',$v,$match);
                    array_push($info['other_report'],['url'=>$v,'name'=>$match[1]]);
                    unset($info['other_report'][$k]);
                }
            }
        }
        $str='';
        if(isset($info['omit']) && $info['omit']){
            $str .=$info['omit'].'  ';
        }
        if(isset($info['city']) && $info['city']){
            $str .=$info['city'].' ';
        }
        if(isset($info['classify']) && $info['classify']){
            $str .=$info['classify'];
        }

        return $this->fetch('recordor/step_3',['policy_num'=>$policy_num,'info'=>$info,'addr'=>$str]);
    }
    /**
     *  保险录入 展示第四步
     */
    public  function step4(){
        $policy_num= input('get.policy_num');
        $info=[];
        if(!empty($policy_num)){
            $info = InsurerApi::getInfoByWherePolicyItem(['policy_num'=>$policy_num], ['present_at,goods_list,goods_desc,present_img,present_video']);
            $info['goods_list'] =  $info['goods_list']?json_decode($info['goods_list']):'';
            $info['present_video'] =  $info['present_video']?json_decode($info['present_video']):'';
            $info['present_img'] =  $info['present_img']?json_decode($info['present_img']):'';
            if( $info['goods_list']){
                foreach ($info['goods_list'] as $k =>$v){
                    if(!$v) continue;
                    preg_match('/\/([^\/]+\.[a-z]+)[^\/]*$/',$v,$match);
                    array_push($info['goods_list'],['url'=>$v,'name'=>$match[1]]);
                    unset($info['goods_list'][$k]);
                }
            }
        }
        return $this->fetch('recordor/step_4',['policy_num'=>$policy_num,'info'=>$info]);
    }
    /**
     *  保险录入 展示第五步
     */
    public  function step5(){
        $policy_num= input('get.policy_num');
        $info=[];
        if(!empty($policy_num)){
            $info = InsurerApi::getInfoByWherePolicyItem(['policy_num'=>$policy_num], ['should_at,rem_company,linkman,connect_phone']);
        }
        return $this->fetch('recordor/step_5',['policy_num'=>$policy_num,'info'=>$info]);
    }





    /*************************************************************************/
    /**
     *  待录人未录入列表
     */
    public function getByme()
    {
        $list = InsurerApi::getByme($this->uid);
//        dump($list);exit;
        return $this->fetch('recordor/ready_recorder',['info'=>$list]);
    }

    /**
     *  历史资料 除了未录入
     */
    public function alreadyList()
    {
         $type=input('get.type');
         if(!$type){
             $type='';
         }
        $list = InsurerApi::alreadyList($this->uid,$type);
        return $this->fetch('recordor/hist',['info'=>$list]);
    }

    /**
     *  历史资料详情
     */
    public function insurerInfo()
    {
        $param=input('param.');
        $pId=$param['p_id'];
//        list($pId) = Validator::make($this->request->request(), [
//            'p_id' => 'required|alpha_num',
//        ]);
        $statusArr = [0 => '未录入', 1 => '未递交', 2 => '已提交保险公司人员未确认', 3 => '驳回', 4 => '已完成'];
        $info = InsurerApi::getRecordorInfo($pId, $this->uid);
        $data = [];
        $data['bohui_info']=[];
        $userModel = New UserInfoModel();
        if (!$info) return $this->response->api($data);
        $userInfo=$userModel->get($info['ui_id']);
        //资料头部信息
        $data['status'] = $info['recoder_status'];
        $data['info']['status_str'] = isset($statusArr[$info['recoder_status']]) ? $statusArr[$info['recoder_status']] : '';
        $data['info']['recordor_commit'] =$info['end_at'];
        $data['info']['creat'] = $info['lu_at'];
        $data['info']['complete_at'] = $info['complete_at'];
        $info['username']= $userInfo['username'];
        $info['mobile']= $userInfo['mobile'];
        $info['company_name']= $userInfo['company_name'];
        if($info['policy_status'] == PolicyModel::POLICY_REVISE_STATUS){
            $data['bohui_info']=InsurerApi::getRetuenList($info['p_id']);
        }
        unset($info['ui_id']);
        unset($info['recordor_id']);
        unset($info['remer_id']);
        unset($info['end_at']);
        unset($info['created_at']);
        unset($info['updated_at']);
        unset($info['delete_ts']);
        unset($info['admin_id']);
        unset($info['policy_status']);
        unset($info['recoder_status']);
        unset($info['pi_id']);
        unset($info['author_report']);
        $info['goods_type_name']=$info['insurance_type_name']='';
        $info['present_img']=$info['present_img']?json_decode($info['present_img']):'';
        $info['present_video']=$info['present_video']?json_decode($info['present_video']):'';
        $info['goods_list'] =  $info['goods_list']?json_decode($info['goods_list']):'';
        $info['other_report']=$info['other_report']?json_decode( $info['other_report']):'';
        if( $info['other_report']){
            foreach ($info['other_report'] as $k =>$v){
                if(!$v) continue;
                preg_match('/\/([^\/]+\.[a-z]+)[^\/]*$/',$v,$match);
                array_push($info['other_report'],['url'=>$v,'name'=>$match[1]]);
                unset($info['other_report'][$k]);
            }
        }
        if($info['goods_list']){
            foreach ($info['goods_list'] as $k =>$v){
                if(!$v) continue;
                preg_match('/\/([^\/]+\.[a-z]+)[^\/]*$/',$v,$match);
                array_push($info['goods_list'],['url'=>$v,'name'=>$match[1]]);
                unset($info['goods_list'][$k]);
            }
        }
        if($info['goods_type']){
            $goodTypeInfo=Db::table('goods_type')->where('gt_id',$info['goods_type'])->find();
            $info['goods_type_name']=$goodTypeInfo['gt_name'];
        }
        if($info['insurance_type']){
            $insuranceTypeInfo=Db::table('insurance_type')->where('it_id','in',$info['insurance_type'])->select();

            foreach ($insuranceTypeInfo as $v){
                $info['insurance_type_name'].= $v['it_name'].',';
            }
            $info['insurance_type_name']= rtrim($info['insurance_type_name'],',');
        }
        $data['goods_info'] = $info;
        return $this->fetch('recordor/insurerInfo',['info'=>$data]);


    }

    /**
     *  代录人 保险录入 修改
     */
    public function recordAdd()
    {

        list($policy_num, $goods_type, $good_name, $goods_num, $good_price,$good_pg_price, $damage_at, $damage_result, $omit,$city,$classify, $address_detail, $other_report, $present_at, $goods_list, $goods_desc, $present_img,$present_video, $should_at, $rem_company, $linkman, $connect_phone, $now_status) = Validator::make($this->request->request(), [
            'policy_num' => 'required|alpha_num',
            'goods_type' => '',
            'goods_name' => '',
            'goods_num' => '',
            'good_price' => '',
            'good_pg_price' => '',
            'damage_at' => '',
            'damage_result' => '',
            'omit' => '',
            'city' => '',
            'classify' => '',
            'address_detail' => '',
            'other_report' => '',
            'present_at' => '',
            'goods_list' => '',
            'goods_desc' => '',
            'present_img' => '',
            'present_video' => '',
            'should_at' => '',
            'rem_company' => '',
            'linkman' => '',
            'connect_phone' => 'cellphone',
            'now_status' => 'required'
        ]);
        $policy_item_data['policy_num'] = $policy_num;
        //副表开始
        if ($goods_type) {
            $policy_item_data['goods_type'] = $goods_type;
        }
        if ($good_name) {
            $policy_item_data['goods_name'] = $good_name;
        }
        if ($goods_num) {
            $policy_item_data['goods_num'] = $goods_num;
        }
        if ($good_price) {
            $policy_item_data['good_price'] = $good_price;
        }
        if ($good_pg_price) {
            $policy_item_data['good_pg_price'] = $good_pg_price;
        }
        if ($damage_at) {
            $policy_item_data['damage_at'] = $damage_at;
        }
        if ($damage_result) {
            $policy_item_data['damage_result'] = $damage_result;
        }
        $policy_item_data['omit'] = $omit;
        if ($city) {
            $policy_item_data['city'] = $city;
        }
        if ($classify) {
            $policy_item_data['classify'] = $classify;
        }
        if ($address_detail) {
            $policy_item_data['address_detail'] = $address_detail;
        }
        if ($other_report) {
            $policy_item_data['other_report'] = $other_report;
        }
        if ($present_at) {
            $policy_item_data['present_at'] = $present_at;
        }
        if ($goods_list) {
            $policy_item_data['goods_list'] = $goods_list;
        }
        if ($goods_desc) {
            $policy_item_data['goods_desc'] = $goods_desc;
        }
        if ($present_img) {
            $policy_item_data['present_img'] = $present_img;
        }
        if ($present_video) {
            $policy_item_data['present_video'] = $present_video;
        }
        if ($should_at) {
            $policy_item_data['should_at'] = $should_at;
        }
        if ($rem_company) {
            $policy_item_data['rem_company'] = $rem_company;
        }
        if ($linkman) {
            $policy_item_data['linkman'] = $linkman;
        }
        if ($connect_phone) {
            $policy_item_data['connect_phone'] = $connect_phone;
        }

        $re = InsurerApi::insertItemPolicy($this->uid, $policy_num, $now_status, $policy_item_data);
        if ($re) {
            return $this->response->api(['policy_num' => $policy_num], self::SUCCESS_CODE, '保存成功');
        } else {
            return $this->response->api([], self::ERROR_CODE, '保存失败');
        }
    }

    /**
     *  代录人 保险录入 第一页数据
     */
    public function recordAddBefor()
    {
        list($pId) = Validator::make($this->request->request(), [
            'p_id' => 'required|alpha_num',
        ]);
        $info = InsurerApi::getPolicyInfo(['policy_num', 'report_num', 'insurance_type', 'ui_id', 'end_at'], ['p_id'=>$pId,'recordor_id' => $this->uid, 'policy_status' => 2, 'now_status' => 1,'delete_ts'=>0]);
        if (!$info) {
            return $this->response->api([], self::ERROR_CODE, '数据错误');
        }
        $userInfo = UserApi::getUserInfoByUid($info['ui_id'], ['username', 'mobile', 'company_name']);

        return $this->response->api(array_merge($info, $userInfo));
    }


    /**
     *  代录人 提交给保险人审核
     */
    public function recordConfir()
    {
        list($policy_num) = Validator::make($this->request->request(), [
            'policy_num' => 'required|alpha_num',
        ]);
        // 确认提交未待审核状态
        $re = InsurerApi::updatePolicy(['recordor_id' => $this->uid, 'policy_num' => $policy_num], ['policy_status' => 3, 'recoder_status' => 2, 'updated_at' => date('Y-m-d H:i:s')]);
        if ($re !== false) {
            return $this->response->api([], self::SUCCESS_CODE, '提交成功');
        } else {
            return $this->response->api([], self::ERROR_CODE, '提交成功');
        }
    }

    /**
     *  修改提交
     */
    public function confirmSubmit()
    {

        list($policy_num, $should_at,$rem_company,$linkman,$connect_phone) = Validator::make($this->request->request(), [
            'policy_num' => 'required',
            'should_at' => '',
            'rem_company' => '',
            'linkman' => '',
            'connect_phone' => 'cellphone',
        ]);
        $now_status = 5;
        $updateArr= ['policy_status' => 3, 'recoder_status' => 2, 'updated_at' => date('Y-m-d H:i:s'),'now_status'=>$now_status];
        $updateArrItem=[];
        if($should_at){
            $updateArrItem['should_at']=$should_at;
        }
        if($rem_company){
            $updateArrItem['rem_company']=$rem_company;
        }
        if($linkman){
            $updateArrItem['linkman']= $linkman;
        }
        if($connect_phone){
            $updateArrItem['connect_phone']= $connect_phone;
        }
        if($updateArrItem){
            $updateArrItem['updated_at']=date('Y-m-d H:i:s');
        }

        // 确认提交未待审核状态
        $re = InsurerApi::updatePolicy(['recordor_id' => $this->uid, 'policy_num' => $policy_num], $updateArr);
        $r=Db::table('policy_item')->where('policy_num',$policy_num)->update($updateArrItem);
        if ($re !== false && $r !== false) {
            return $this->response->api([], self::SUCCESS_CODE, '提交成功');
        } else {
            return $this->response->api([], self::ERROR_CODE, '提交成功');
        }

    }

    /**
     *  代录人 保险录入 修改
     */
    public function update()
    {

        list($policy_num, $goods_type, $good_name, $goods_num, $good_price,$good_pg_price, $damage_at, $damage_result, $omit,$city,$classify, $address_detail, $other_report, $present_at, $goods_list, $goods_desc, $present_img,$present_video, $should_at, $rem_company, $linkman, $connect_phone) = Validator::make($this->request->request(), [
            'policy_num' => 'required|alpha_num',
            'goods_type' => '',
            'goods_name' => '',
            'goods_num' => '',
            'good_price' => '',
            'good_pg_price' => '',
            'damage_at' => '',
            'damage_result' => '',
            'omit' => '',
            'city' => '',
            'classify' => '',
            'address_detail' => '',
            'other_report' => '',
            'present_at' => '',
            'goods_list' => '',
            'goods_desc' => '',
            'present_img' => '',
            'present_video' => '',
            'should_at' => '',
            'rem_company' => '',
            'linkman' => '',
            'connect_phone' => 'cellphone',
        ]);
        $policy_item_data['policy_num'] = $policy_num;
        //副表开始
        if ($goods_type) {
            $policy_item_data['goods_type'] = $goods_type;
        }
        if ($good_name) {
            $policy_item_data['goods_name'] = $good_name;
        }
        if ($goods_num) {
            $policy_item_data['goods_num'] = $goods_num;
        }
        if ($good_price) {
            $policy_item_data['good_price'] = $good_price;
        }
        if ($good_pg_price) {
            $policy_item_data['good_pg_price'] = $good_pg_price;
        }
        if ($damage_at) {
            $policy_item_data['damage_at'] = $damage_at;
        }
        if ($damage_result) {
            $policy_item_data['damage_result'] = $damage_result;
        }
        $policy_item_data['omit'] = $omit;
        if ($city) {
            $policy_item_data['city'] = $city;
        }
        if ($classify) {
            $policy_item_data['classify'] = $classify;
        }
        if ($address_detail) {
            $policy_item_data['address_detail'] = $address_detail;
        }
        if ($other_report) {
            $policy_item_data['other_report'] = $other_report;
        }
        if ($present_at) {
            $policy_item_data['present_at'] = $present_at;
        }
        if ($goods_list) {
            $policy_item_data['goods_list'] = $goods_list;
        }
        if ($goods_desc) {
            $policy_item_data['goods_desc'] = $goods_desc;
        }
        if ($present_img) {
            $policy_item_data['present_img'] = $present_img;
        }
        if ($present_video) {
            $policy_item_data['present_video'] = $present_video;
        }
        if ($should_at) {
            $policy_item_data['should_at'] = $should_at;
        }
        if ($rem_company) {
            $policy_item_data['rem_company'] = $rem_company;
        }
        if ($linkman) {
            $policy_item_data['linkman'] = $linkman;
        }
        if ($connect_phone) {
            $policy_item_data['connect_phone'] = $connect_phone;
        }

        $re = InsurerApi::updateItemPolicy($this->uid, $policy_num, $policy_item_data);
        if ($re) {
            return $this->response->api(['policy_num' => $policy_num], self::SUCCESS_CODE, '保存成功');
        } else {
            return $this->response->api([], self::ERROR_CODE, '保存失败');
        }
    }

}
